#!/bin/bash

# Slackware build for mod_fcgid (to be used with Apache 2.2)
# Version: 2.1.0 Date: 2007-02-27
# Copyright (c) 2007 Adis Nezirovic.<adis _at_ linux.org.ba>
# Licensed under GNU GPL v2

# Slightly modified by the slackbuild project
set -e


PRGNAM=mod_fcgid
VERSION=2.1
# CFLAGS are hardcoded in /usr/lib/apr-1.2.x/build-1/apr_rules.mk
# and ARCH should reflect that, so don't change it ;-)
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Other important variables
APACHE2_PREFIX=usr
APACHE2_LIBEXEC=usr/libexec/apache2
APACHE2_ETC=etc/apache2
APACHE2_VAR_RUN=var/run/apache2

rm -rf $TMP/$PRGNAM.$VERSION $PKG
mkdir -p $TMP $PKG $OUTPUT 
mkdir -p $PKG/{$APACHE2_LIBEXEC,$APACHE2_ETC/extra,$APACHE2_ETC/original/extra,$APACHE2_VAR_RUN}

cd $TMP
tar xzvf $CWD/$PRGNAM.$VERSION.tar.gz
cd $TMP/$PRGNAM.$VERSION
chmod -R a-s,u+w,go+r-w .
chown -R root:root .

make top_dir=/$APACHE2_PREFIX top_builddir=/$APACHE2_LIBEXEC top_srcdir=/$APACHE2_LIBEXEC || exit 1
# make install spams the root partition, this is cleaner :-)
/$APACHE2_LIBEXEC/build/instdso.sh SH_LIBTOOL=libtool $PRGNAM.la $PKG/$APACHE2_LIBEXEC || exit 1

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

## mod_fcgid writes socket to this directory
mkdir -p $PKG/$APACHE2_VAR_RUN/fcgid/fcgidsock
chown -R apache2:apache2 $PKG/$APACHE2_VAR_RUN/fcgid
chmod -R 700 $PKG/$APACHE2_VAR_RUN/fcgid

## mod_fcgid config
cat $CWD/httpd-fcgid.conf > $PKG/$APACHE2_ETC/extra/httpd-fcgid.conf.new
cat $CWD/httpd-fcgid.conf > $PKG/$APACHE2_ETC/original/extra/httpd-fcgid.conf

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
